package com.amrit.basic;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.codec.binary.Base64;


public final class MyPasswordEncrypt {
	public static synchronized String encrypt(String plaintext, String algorithm, String encoding) throws Exception {
		MessageDigest msgDigest = null;
		String hashValue = null;
		try {
			msgDigest = MessageDigest.getInstance(algorithm);
			msgDigest.update(plaintext.getBytes(encoding));
			byte rawByte[] = msgDigest.digest();
			hashValue = new Base64().encodeToString(rawByte);

		} catch (NoSuchAlgorithmException e) {
			System.out.println("No Such Algorithm Exists");
		} catch (UnsupportedEncodingException e) {
			System.out.println("The Encoding Is Not Supported");
		}
		return hashValue;
	}

	public static void main(String args[]) throws Exception {
		String plainPassword = "Password@123";
		System.out.println("PlainText\tAlgo\tEncoding\tEncrypted Password");
		System.out.println(plainPassword + "\tSHA-256\tUTF-8\t" + encrypt(plainPassword, "SHA-256", "UTF-8"));
		System.out.println(plainPassword + "\tSHA-1\tUTF-16\t" + encrypt(plainPassword, "SHA-1", "UTF-16"));
		System.out.println(plainPassword + "\tMD5\tUTF-8\t" + encrypt(plainPassword, "MD5", "UTF-8"));
		System.out.println(plainPassword + "\tMD5\tUTF-16\t" + encrypt(plainPassword, "MD5", "UTF-16"));

	}
}
